package com.mask.acl.controller;


import com.mask.acl.entity.Permission;
import com.mask.acl.service.PermissionService;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.util.RollResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限 菜单管理
 * </p>
 *
 * @author hx
 */
@RestController
@RequestMapping("/admin/acl/permission")
@Api(tags = "菜单权限管理")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "查询所有菜单")
    @GetMapping("/menu")
    public RollResponse indexAllPermission() {
        List<Permission> list =  permissionService.queryAllMenu();
        return RollResponse.result(ServiceEnums.ACL_QUERY_ALL_MENU_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.ACL_QUERY_ALL_MENU_SUCCESS.getMessage(),list);
    }

    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("/remove/{id}")
    public RollResponse remove(@PathVariable String id) {
        permissionService.removeChildById(id);
        return RollResponse.result(ServiceEnums.ACL_REMOVE_MENU_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.ACL_REMOVE_MENU_SUCCESS.getMessage());
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/assign")
    public RollResponse doAssign(String roleId,String[] permissionId) {
        permissionService.saveRolePermissionRealtionShip(roleId,permissionId);
        return RollResponse.result(ServiceEnums.ACL_ASSIGN_ROLE_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.ACL_ASSIGN_ROLE_SUCCESS.getMessage());
    }

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/get/assign/{roleId}")
    public RollResponse toAssign(@PathVariable String roleId) {
        List<Permission> list = permissionService.selectMenuByRole(roleId);
        return RollResponse.result(ServiceEnums.ACL_GET_PERMISSION_BY_ROLE.getCode(),
                RollConstants.SUCCESS,ServiceEnums.ACL_GET_PERMISSION_BY_ROLE.getMessage(),list);
    }



    @ApiOperation(value = "新增菜单")
    @PostMapping("/save")
    public RollResponse save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return RollResponse.result(ServiceEnums.ACL_ADD_MENU_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.ACL_ADD_MENU_SUCCESS.getMessage());
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("/update")
    public RollResponse updateById(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return RollResponse.result(ServiceEnums.ACL_UPDATE_MENU_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.ACL_UPDATE_MENU_SUCCESS.getMessage());
    }

}

